#include "structure.h"

static uLL hash(const char * ch){
    uLL val=0,i;
    const uLL size=0x3fff;
    for(;*ch;ch++){
        val=(val<<2)+*ch;
        if((i=val)&~size) val=(val^(i>>12))&size;
    }
    return val;
}

static int total_var=0,total_label=0;

struct unit{
    const char * name;
    int id;
};
List var_list[0x4000],label_list[0x4000];

static int insert(const char * name, List * a, int * num){
    int id=hash(name);
    FOR_LIST(now,a[id]) if(strcmp(LIST_GET(now,struct unit * )->name,name)==0) return LIST_GET(now,struct unit *)->id;
    NEW_V(now,struct unit,.id=++(*num),.name=deep_copy(name))
    LIST_INSERT(a[id],now);
    return now->id;
}

int insert_var(const char * name) {return insert(name,var_list,&total_var);}
int insert_label(const char * name) {return insert(name,label_list,&total_label);}
